package com.chenfei.sharding.jdbc.controller;

import com.chenfei.sharding.jdbc.entity.Student;
import com.chenfei.sharding.jdbc.request.StudentReq;
import com.chenfei.sharding.jdbc.service.StudentService;
import groovy.util.logging.Slf4j;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author chenfei
 * @description
 * @since 2018/3/30
 */
@Api(value = "分库控制器",description="分库控制器")
@Slf4j
@RestController
public class StudentController {

    @Autowired
    StudentService studentService;

    @ApiOperation(value = "插入数据", httpMethod = "POST")
    @ApiResponse(code = 200, message = "success")
    @RequestMapping(value = "/students",method = RequestMethod.POST)
    public String addStudent(@RequestBody StudentReq studentReq){
        Student student = new Student();
        BeanUtils.copyProperties(studentReq,student);
        studentService.insert(student);
        return "success";
    }

    @ApiOperation(value = "获取数据", httpMethod = "GET")
    @ApiResponse(code = 200, message = "success")
    @RequestMapping(value = "/students",method = RequestMethod.GET)
    public Student findStudent(@RequestParam Integer id){
        Student student = studentService.find(id);
        return student;
    }
}
